package cn.miao.middle.cloud.core.model;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

import cn.miao.middle.cloud.core.auth.details.DefaultClientDetails;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 应用实体
 * 
 * @author kongyufei
 *
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "应用实体类", description = "应用实体类对象")
@TableName("sys_client")
public class SysClient implements Serializable {
	private static final long serialVersionUID = -8185413579135897885L;

	@JsonSerialize(using = ToStringSerializer.class)
	@ApiModelProperty(value = "主键ID")
	private Long id;
	
	@ApiModelProperty(value = "应用id")
	private String clientId;
	
	@ApiModelProperty(value = "应用秘钥")
	private String clientSecret;
	
	@ApiModelProperty(value = "应用秘钥字符串")
	private String clientSecretStr;
	
	@ApiModelProperty(value = "作用域")
	private String scope = "all";
	
	@ApiModelProperty(value = "资源Ids")
	private String resourceIds = "";
	
	@ApiModelProperty(value = "认证授权类型")
	private String authorizedGrantTypes = "authorization_code,password,refresh_token,client_credentials";
	
	@ApiModelProperty(value = "跳转地址")
	private String webServerRedirectUri;
	
	@ApiModelProperty(value = "授权")
	private String authorities = "";
	
	@ApiModelProperty(value = "accessToken有效时间")
	private Integer accessTokenValidity = 18000;
	
	@ApiModelProperty(value = "refreshToken有效时间")
	private Integer refreshTokenValidity = 18000;
	
	@ApiModelProperty(value = "附加信息")
	private String additionalInformation = "{}";
	
	@ApiModelProperty(value = "自动通过")
	private String autoapprove = "true";
	
	@ApiModelProperty(value = "状态")
	private Boolean status;
	
	@ApiModelProperty(value = "限流标识")
	private Integer ifLimit;
	
	@ApiModelProperty(value = "限流次数")
	@JsonSerialize(using = ToStringSerializer.class)
	private Long limitCount = 10000L;
	
	@ApiModelProperty(value = "权限id集合")
	private List<Long> permissionIds;
	
	@ApiModelProperty(value = "服务id集合")
	private Set<Long> serviceIds;

	public DefaultClientDetails map() {
		DefaultClientDetails defaultClientDetails = new DefaultClientDetails(this.clientId, this.resourceIds,
				this.scope, this.authorizedGrantTypes, this.authorities, this.webServerRedirectUri);
		defaultClientDetails.setId(this.id);
		defaultClientDetails.setClientSecret(this.clientSecret);
		defaultClientDetails.setAccessTokenValiditySeconds(this.accessTokenValidity);
		defaultClientDetails.setRefreshTokenValiditySeconds(this.refreshTokenValidity);
		defaultClientDetails.setAutoApproveScopes(StringUtils.commaDelimitedListToSet(this.scope));
		defaultClientDetails.setIfLimit(this.ifLimit);
		defaultClientDetails.setLimitCount(this.limitCount);

		return defaultClientDetails;
	}
}
